package com.syni.mdd.yhd.common.all.aop;

import com.syni.mdd.yhd.common.all.annotation.ControllerLog;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsUserOperationLogDao;
import com.syni.mdd.yhd.common.all.entity.BmsUserOperationLog;
import com.syni.mdd.yhd.common.all.utils.TokenManager;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * @className ControllerLogAspect
 * @description 记录用户请求aop(后期)
 * @Author cfx
 * @DATE 2019/4/9 10:51
 * @VERSION 1.0
 **/
public class ControllerLogAspect {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsUserOperationLogDao bmsUserOperationLogDao;

    @Before("@annotation(controllerLog)")
    public void before(JoinPoint joinPoint, ControllerLog controllerLog){
        Object[] args = joinPoint.getArgs();
        if(args.length > 0){
            //System.out.println(bmsLogDao);
            Map<String,Object> argsMap = (Map<String, Object>) args[0];
            if(argsMap.containsKey("open_id")){
                //后台点击类型
                Integer operation_type = 0;
                Integer userId = TokenManager.getUserId(argsMap.get("open_id").toString());
                if(userId != null){
                    BmsUserOperationLog bmsUserOperationLog = new BmsUserOperationLog();
                    bmsUserOperationLog.setUserId(userId);
                    bmsUserOperationLog.setOperationTime(new Date());
                    bmsUserOperationLog.setOperationType(operation_type);
                    bmsUserOperationLog.setOperationName(controllerLog.operationAction());
                    bmsUserOperationLogDao.save(bmsUserOperationLog);
                    log.info("用户："+ userId + " 访问了：" + controllerLog.operationAction());
                }
            }
        }
    }
}
